﻿//  Symplectic Spark
//  Copyright 2010 Symplectic Ltd
//  Created by Martyn Whitwell (martyn@symplectic.co.uk)

//  This file is part of Spark.

//  Spark is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  Spark is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with Spark.  If not, see <http://www.gnu.org/licenses/>.

// $URL: https://symplectic-spark.googlecode.com/svn/trunk/website/App_Code/Templater.cs $
// $LastChangedDate: 2010-03-02 15:38:13 +0000 (Tue, 02 Mar 2010) $
// $LastChangedRevision: 11 $
// $LastChangedBy: martyn@symplectic.co.uk $

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;


namespace Symplectic.Spark
{
    /// <summary>
    /// Summary description for Templater
    /// </summary>
    public class Templater
    {
        private RequestSpec RequestSpec { get; set; }
        private Template Template { get; set; }


        public Templater(RequestSpec requestSpec)
        {
            RequestSpec = requestSpec;
            if (requestSpec.IsError)
                Template = Global.ErrorTemplate;
            else
                Template = Global.Templates[RequestSpec.CurrentElementTypeTemplate.Name];
        }

        public Templater(RequestSpec requestSpec, Template template)
        {
            RequestSpec = requestSpec;
            Template = template;
        }

       
        private string _content = null;
        public string Content
        {
            get
            {
                if (_content == null)
                {
                    //Get list of components used in the template
                    //NB: this IS case sensitive

                    StringBuilder sbContent = new StringBuilder(Template.Markup);
                    
                    //Need to do error values first
                    if (RequestSpec.IsError && RequestSpec.Exception != null)
                    {
                        sbContent.Replace(@"[[error-message]]", RequestSpec.Exception.Message);
                        sbContent.Replace(@"[[error-stack-trace]]", RequestSpec.Exception.StackTrace);
                        sbContent.Replace(@"[[error-source]]", RequestSpec.Exception.Source);
                    }

                    //Populate Components
                    foreach (string componentName in Template.ComponentNames)
                        sbContent.Replace(@"[[" + componentName + @"]]",
                            Global.Components.ContainsKey(componentName) ? Global.Components[componentName].GetValue(RequestSpec) : "[" + componentName + " is not defined]");
                    
                    //Populate Internal Functions
                    foreach (string internalFunctionName in Template.InternalFunctionNames)
                        sbContent.Replace(@"((" + internalFunctionName + @"))", InternalFunctions.GetValue(internalFunctionName, RequestSpec));
                   
                    //Populate RequestSpecs
                     foreach (string requestSpecName in Template.RequestSpecNames)
                        sbContent.Replace(@"{{" + requestSpecName + @"}}", RequestSpec[requestSpecName]);
                   
                    _content = sbContent.ToString();
                }
                return _content;
            }
        }
    }
}
